package com.isunland.managesystem.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseRecoverTaskFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 追回
 *
 * @author Administrator
 */
public class RecoverTaskFragment extends BaseRecoverTaskFragment {
  public static String EXTRA_TASK_ID =
      "com.isunland.managesystem.ui.RecoverTaskFragment.EXTRA_TASK_ID";
  private String mTaskId;

  @Override protected void setTitle() {

    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle("追回原因");
  }

  @Override protected void initData() {
    mTaskId = getActivity().getIntent().getStringExtra(EXTRA_TASK_ID);
    mcbAlertPhone.setVisibility(View.GONE);
    //提醒消息
    mcbAlertStation.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //如果被选中
        if (isChecked) {
          type1 = "3";
        } else {
          type1 = "";
        }
      }
    });
    super.initData();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

        break;
      case R.id.menu_item_confirm:
        if (TextUtils.isEmpty(metChangeCause.getText().toString())) {
          Toast.makeText(getActivity(), R.string.notComplete, Toast.LENGTH_SHORT).show();
        } else {
          volleySave();
        }

        break;

      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  private void volleySave() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_TASK_RECOVER);
    HashMap<String, String> params = new HashMap<String, String>();
    if (!"".equals(type1)) {
      params.put("informType", type1);
    }
    params.put("opinion", metChangeCause.getText().toString());
    params.put("runId", mTaskId);
    params.put("backToStart", "1");//固定数字1代表追回
    LogUtil.i("追回params===" + params.toString());
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        try {
          LogUtil.i("追回arg0===" + arg0);
          MyUtils.dismissDialog();
          //解析
          SuccessMessage result = new Gson().fromJson(arg0, SuccessMessage.class);
          if (null == result || null == result.getResult()) {
            Toast.makeText(getActivity(), R.string.wrong_data, Toast.LENGTH_SHORT).show();
            return;
          }
          String successType = result.getResult();
          if (successType.equals(SuccessMessage.FAILURE)) {
            Toast.makeText(getActivity(), result.getMessage().toString(), Toast.LENGTH_SHORT)
                .show();
            return;
          }
          if (successType.equals(SuccessMessage.SUCCESS)) {
            Toast.makeText(getActivity(), result.getMessage().toString(), Toast.LENGTH_SHORT)
                .show();

            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
          }
        } catch (JsonSyntaxException e) {
          Toast.makeText(getActivity(), R.string.wrong_data, Toast.LENGTH_SHORT).show();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
      }
    });
  }

  public static RecoverTaskFragment newInstance(String taskId) {
    Bundle args = new Bundle();
    args.putString(EXTRA_TASK_ID, taskId);
    RecoverTaskFragment fragment = new RecoverTaskFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
